package com.swss.g.sys.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.swss.g.base.impl.BaseDAOImpl;
import com.swss.g.sys.bean.Print;
import com.swss.g.sys.dao.PrintDAO;

public class PrintDAOImpl extends BaseDAOImpl<Print> implements PrintDAO {

	public Print findUsedPrint(int type) throws Exception {
		// TODO Auto-generated method stub
		final String hql = "FROM Print WHERE printType='" + type
		+ "' AND isUsedPrint=1";
		try{
		   
			return this.findOneByHql(hql);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List findPrintByType(int type) throws Exception {
		// TODO Auto-generated method stub
		String hql = "FROM Print WHERE printType='" + type + "'";
		return this.findByHql(hql);
	}

	public boolean updatePrint(final List<Print> prints) throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						try {
							for (Print p : prints) {
								Print print = (Print) session.get(Print.class,
										p.getId());
								print.setIsUsedPrint(p.getIsUsedPrint());
							}
							return true;
						} catch (Exception ex) {
							ex.printStackTrace();
							return false;
						}
					}

				});

		return flag;
	}	
}
